<?php

declare(strict_types=1);

namespace App\Dao;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\CouponBatch as Model;
use App\Model\Coupon;

class CouponBatchDao
{

    use DaoTrait;

    public function __construct()
    {
        $this->driver = __CLASS__;
    }

    /**
     * 根据分页与条件查询数据
     * @Author czm
     * @DateTime 2020-05-26
     * @param array $map
     * @param array $page
     * @param array $fields
     * @return void
     */
    public function pageByWhere($map = [], $page = [], $fields = [], $scope = [])
    {
        $model = Model::query();
        if (!empty($fields)) {
            $model->select($fields);
        }
        $model = $model->when(!empty($map), function ($query) use ($map) {
            $query->where($map);
        });
        $batch = $scope['batch'] ?? '';
        $model = $model->OfLikeField('batch', $batch);
        $count = $model->count();
        $data = $model->orderBy('id', 'desc')
                ->paginate(intval($page['pageSize']),['*'],'page',$page['page'])->map(function ($value) {
                $value['already_use'] = Coupon::where('batch_id', $value->id)->where('status', 2)->count();
                $value['not_use'] = Coupon::where('batch_id', $value->id)->where('status', 1)->count();
                if (time() > $value->end_time) {
                    $value['expire'] = $value['number'] - $value['already_use'];
                } else {
                    $value['expire'] = 0;
                }
                $value->forceFill([
                    'vaildaity_time' => date("Y-m-d H:i:s", $value->start_time) . ' - ' . date("Y-m-d H:i:s", $value->end_time),
                ])->append(['type_mapping'])->makeHidden(['coupon']);
                return $value->toArray();
            });
        return compact('count', 'data');
    }

    //根据条件获取所有内容
    public function getByWhere($map = [], $scope = [])
    {
        $model = Model::query();
        $batch = $scope['batch'] ?? '';
        $model = $model->OfLikeField('batch', $batch);
        return   $model->when(!empty($map), function ($query) use ($map) {
            $query->where($map);
        })->orderBy('id', 'desc')->get()->map(function ($value) {
            $value['already_use'] = Coupon::where('batch_id', $value->id)->where('status', 2)->count();
            $value['not_use'] = Coupon::where('batch_id', $value->id)->where('status', 1)->count();
            if (time() > $value->end_time) {
                $value['expire'] = $value['number'] - $value['already_use'];
            } else {
                $value['expire'] = 0;
            }
            $value->forceFill([
                'vaildaity_time' => date("Y-m-d H:i:s", $value->start_time) . ' - ' . date("Y-m-d H:i:s", $value->end_time),
            ])->append(['type_mapping'])->makeHidden(['coupon']);
            return $value->toArray();
        });
    }

    //返回一个当前日期最新批次码
    public function getBatch($batch){
        return  Model::OfLikeField('batch', $batch)->orderBy('batch', 'desc')->value('batch');
    }
}
